package com.msb.mall.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;
import com.msb.mall.product.dao.CategoryBrandRelationDao;
import com.msb.mall.product.entity.BrandEntity;
import com.msb.mall.product.entity.CategoryBrandRelationEntity;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.BrandService;
import com.msb.mall.product.service.CategoryBrandRelationService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    CategoryService categoryService;
    @Autowired
    BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据类别编号
     * @param categoryBrandRelation
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //根据类别编号和品牌编号找到对应的 类别名称和品牌名称
        CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCategoryByName(Long categoryId, String categoryName) {
        CategoryBrandRelationEntity entity =new CategoryBrandRelationEntity();
        entity.setCatelogId(categoryId);
        entity.setCatelogName(categoryName);
        this.update(entity
                ,new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id",categoryId ));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBrandByName(Long brandId, String brandName) {
        CategoryBrandRelationEntity entity =new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(brandName);
        this.update(entity
                ,new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id",brandId ));
    }


    /**
     * 根据类别查询品牌信息
     * @param catId 类别id
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {
        //找到所有的品牌信息
        return this.list(
                new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("catelog_id", catId)
        );
    }

}
